package gui;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.JLabel;

import probability.Probability;

import experiment.Experiment;

import army.Army;

public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField a0Field;
	private JTextField b0Field;
	private JTextField a1Field;
	private JTextField a2Field;
	private JTextField a3Field;
	private JTextField a4Field;
	private JTextField b1Field;
	private JTextField b2Field;
	private JTextField b3Field;
	private JTextField b4Field;
	private JTextField experimentResult;
	private JTextField calculateResult;
	private JTextField numOfExperimentField;
	private JButton experimentButton;
	private JButton calcButton;
	private Army aliance;
	private Army axis;
	private JButton btnInfo;
	private int numOfExperiments;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI frame = new GUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 213, 492);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		a0Field = new JTextField("0");
		a0Field.setColumns(10);
		a0Field.setBounds(76, 85, 43, 20);
		a0Field.setHorizontalAlignment(JTextField.TRAILING);
		a0Field.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==KeyEvent.VK_ENTER){
					makeExperement();
				}
			}
		});
		contentPane.add(a0Field);
		
		a1Field = new JTextField("0");
		a1Field.setBounds(76, 116, 43, 20);
		a1Field.setColumns(10);
		a1Field.setHorizontalAlignment(JTextField.TRAILING);
		contentPane.add(a1Field);
		
		a2Field = new JTextField("10");
		a2Field.setBounds(76, 147, 43, 20);
		a2Field.setHorizontalAlignment(JTextField.TRAILING);
		a2Field.setColumns(10);
		contentPane.add(a2Field);
		
		a3Field = new JTextField("0");
		a3Field.setBounds(76, 178, 43, 20);
		a3Field.setHorizontalAlignment(JTextField.TRAILING);
		a3Field.setColumns(10);
		contentPane.add(a3Field);
		
		a4Field = new JTextField("0");
		a4Field.setBounds(76, 209, 43, 20);
		a4Field.setHorizontalAlignment(JTextField.TRAILING);
		a4Field.setColumns(10);
		contentPane.add(a4Field);
		
		b0Field = new JTextField("0");
		b0Field.setColumns(10);
		b0Field.setBounds(132, 85, 43, 20);
		b0Field.setHorizontalAlignment(JTextField.TRAILING);
		contentPane.add(b0Field);
		
		b1Field = new JTextField("0");
		b1Field.setBounds(132, 116, 43, 20);
		b1Field.setHorizontalAlignment(JTextField.TRAILING);
		b1Field.setColumns(10);
		contentPane.add(b1Field);
		
		b2Field = new JTextField("0");
		b2Field.setBounds(132, 147, 43, 20);
		b2Field.setHorizontalAlignment(JTextField.TRAILING);
		b2Field.setColumns(10);
		contentPane.add(b2Field);
		
		b3Field = new JTextField("6");
		b3Field.setBounds(132, 178, 43, 20);
		b3Field.setHorizontalAlignment(JTextField.TRAILING);
		b3Field.setColumns(10);
		contentPane.add(b3Field);
		
		b4Field = new JTextField("0");
		b4Field.setBounds(132, 209, 43, 20);
		b4Field.setHorizontalAlignment(JTextField.TRAILING);
		b4Field.setColumns(10);
		contentPane.add(b4Field);
		
		experimentResult = new JTextField("");
		experimentResult.setBounds(20, 300, 155, 20);
		experimentResult.setHorizontalAlignment(JTextField.TRAILING);
		experimentResult.setHorizontalAlignment(JTextField.TRAILING);
		experimentResult.setColumns(10);
		contentPane.add(experimentResult);

		experimentButton = new JButton("Experiment");
		experimentButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				makeExperement();
			}
		});
		experimentButton.setBounds(50, 330, 125, 25);
		contentPane.add(experimentButton);

		calculateResult = new JTextField("now just only for 1st round");
		calculateResult.setBounds(20, 382, 153, 20);
		calculateResult.setHorizontalAlignment(JTextField.TRAILING);
		calculateResult.setColumns(10);
		contentPane.add(calculateResult);

		numOfExperimentField = new JTextField("10000");
		numOfExperimentField.setColumns(10);
		numOfExperimentField.setBounds(20, 264, 155, 20);
		numOfExperimentField.setHorizontalAlignment(JTextField.TRAILING);
		contentPane.add(numOfExperimentField);
		
		calcButton = new JButton("Calculate");
		calcButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calculate();
			}

		});
		calcButton.setBounds(50, 413, 125, 23);
		contentPane.add(calcButton);
		
		JLabel lblAttack = new JLabel("Attack");
		lblAttack.setBounds(20, 60, 46, 14);
		contentPane.add(lblAttack);
		
		JLabel label_0 = new JLabel("0");
		label_0.setBounds(30, 88, 16, 14);
		contentPane.add(label_0);
		
		JLabel label_1 = new JLabel("1");
		label_1.setBounds(30, 119, 16, 14);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("2");
		label_2.setBounds(30, 150, 16, 14);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("3");
		label_3.setBounds(30, 181, 16, 14);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("4");
		label_4.setBounds(30, 212, 16, 14);
		contentPane.add(label_4);
		
		JLabel lblAliance = new JLabel("Aliance");
		lblAliance.setBounds(76, 60, 46, 14);
		contentPane.add(lblAliance);
		
		JLabel lblAxis = new JLabel("Axis");
		lblAxis.setBounds(132, 60, 46, 14);
		contentPane.add(lblAxis);
		
		JLabel lblExperiment = new JLabel("Experiment Result");
		lblExperiment.setBounds(20, 286, 153, 14);
		contentPane.add(lblExperiment);
		
		JLabel lblMathExpectation = new JLabel("MathExpectation");
		lblMathExpectation.setBounds(20, 369, 155, 14);
		contentPane.add(lblMathExpectation);
		
		JLabel lblNumofexperiments = new JLabel("NumOfExperiments");
		lblNumofexperiments.setBounds(20, 250, 155, 14);
		contentPane.add(lblNumofexperiments);
		
		btnInfo = new JButton("Info");
		btnInfo.setEnabled(false);
		btnInfo.setBounds(20, 21, 155, 23);
		contentPane.add(btnInfo);
	
	}

	public void parseArmy (){
		int a0, a1, a2, a3, a4;
		int b0, b1, b2, b3, b4;
		try{
			a0 = Integer.parseInt(a0Field.getText());
			a1 = Integer.parseInt(a1Field.getText());
			a2 = Integer.parseInt(a2Field.getText());
			a3 = Integer.parseInt(a3Field.getText());
			a4 = Integer.parseInt(a4Field.getText());
			b0 = Integer.parseInt(b0Field.getText());
			b1 = Integer.parseInt(b1Field.getText());
			b2 = Integer.parseInt(b2Field.getText());
			b3 = Integer.parseInt(b3Field.getText());
			b4 = Integer.parseInt(b4Field.getText());
			
			numOfExperiments = Integer.parseInt(numOfExperimentField.getText());
			
		} catch (NumberFormatException numExc) {
			experimentResult.setText("parse error");
			return;
		}
		
		Random random = new Random(System.nanoTime());
		aliance = new Army(a0, a1, a2, a3, a4, random);
		axis 	= new Army(b0, b1, b2, b3, b4, random);
	}
		
	public void makeExperement() {
		parseArmy();
		Experiment experiment = new Experiment(aliance, axis);
		double experimentExpectation = experiment.makeExperiment(numOfExperiments);		
		experimentResult.setText( ((Double)experimentExpectation).toString() );
	}
	
	public void calculate() {
		parseArmy();
		
		System.out.println("Probability distribution table for 1st round");
		
		Probability army1Probability = new Probability(aliance);
		army1Probability.calculateDistribution();
		System.out.println("Army1");
		army1Probability.printDistribution();
		System.out.println("math expectation = " + army1Probability.mathExpectation() );
		
		Probability army2Probability = new Probability(axis);
		System.out.println("Army2");
		army2Probability.calculateDistribution();
		army2Probability.printDistribution();
		System.out.println("math expectation = " + army2Probability.mathExpectation() );
		
		calculateResult.setText("results in console" );
	}

}
